package com.itboye.shop;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.itboye.shop.base.BaseFragment;
import com.itboye.shop.logincontroller.LoginController;
import com.itboye.shop.me.ActivitySetUp;
import com.itboye.shop.me.LoginActivity;
import com.itboye.shop.util.helper.ImageLoader;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.SPUtils;

/**
 * 我的
 */
public class MeFragment extends BaseFragment {

    TextView add_shap_title_tv;
    ImageView close_icon;
    SimpleDraweeView simpleDraweeView;
    TextView user_name, user_num, myorder, address, my_wallet, help_tv;
    ImageView img_setting;
    RelativeLayout top;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_person, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.top://个人信息
                LoginController.PersonnalInfomaton(getActivity(), null);
                break;
            case R.id.myorder://我的订单
                LoginController.onDingDan(getActivity(), null);
                break;
            case R.id.address://地址管理
                LoginController.onShouHuoDiZhi(getActivity(), null);
                break;
            case R.id.my_wallet://我的钱包
                LoginController.onQianBao(getActivity(), null);
                break;
            case R.id.help_tv://帮助中心
                intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.img_setting://设置
                intent = new Intent(getActivity(), ActivitySetUp.class);
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        read();

        //退出登录的广播
        IntentFilter intentFilterExit = new IntentFilter(
                "EXITCHANGE");
        getActivity()
                .registerReceiver(tuichuReceiver, intentFilterExit);
    }
    BroadcastReceiver tuichuReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            read();
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(tuichuReceiver);
    }

    /**
     * 加入数据
     */
    private void read() {
        if ((boolean) SPUtils.get(getActivity(), null, Const.IS_LOGINED, false)) {
            String nikeName = (String) SPUtils.get(getActivity(), null,
                    Const.NICK, "");
            user_name.setText("用户名：" + nikeName);
            String mobile = (String) SPUtils.get(getActivity(), null,
                    Const.MOBILE, "");
            user_num.setText(mobile);
            String head = (String) SPUtils.get(getActivity(), null, Const.HEAD, "");
            if (head != null) {
                ImageLoader.loadImage(simpleDraweeView, Const.IMAGE_HEAD + head);
            } else {
                simpleDraweeView.setImageResource(R.drawable.default_head);
            }
        } else {
            user_name.setText("");
            user_num.setText("");
            simpleDraweeView.setImageResource(R.drawable.default_head);
        }
    }
}
